﻿using A2.ERP.BLL;
using A2.ERP.Models;
using System;
using System.Data.SqlClient;
using System.Linq;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace A2.ERP.UI.Controllers
{
    public class RepertoryController : Controller
    {
        // GET: /Repertory/

        #region server
        private readonly IBLL.It_StoreHouseDetailService _StoreHouseDetail = DBService.GetService<t_StoreHouseDetailService>();
        private readonly IBLL.It_InventoryService Inventory = DBService.GetService<t_InventoryService>();
        private readonly IBLL.It_UserService _User = DBService.GetService<t_UserService>();
        private readonly IBLL.It_ProductsService _Products = DBService.GetService<t_ProductsService>();
        private readonly IBLL.It_StoreHouseService _StoreHouse = DBService.GetService<t_StoreHouseService>();
        private readonly IBLL.It_OutStockService _OutStock = DBService.GetService<t_OutStockService>();
        private readonly IBLL.It_AppendStockService _AppendStock = DBService.GetService<t_AppendStockService>();
        private readonly IBLL.It_BuyReturnService _BuyReturn = DBService.GetService<t_BuyReturnService>();


        //存储过程
        private readonly IBLL.Ip_ProductsStock_getDataSetByHouseDetailID_ResultService _vProductsStock = DBService.GetService<p_ProductsStock_getDataSetByHouseDetailID_ResultService>();
        private readonly IBLL.It_StoreHouseService _storeHouseService = DBService.GetService<t_StoreHouseService>();
        private readonly IBLL.It_StoreHouseDetailService _storeHouseDetail = DBService.GetService<t_StoreHouseDetailService>();
        private readonly IBLL.Ip_ProductsStock_getDataSetByProductsID_ResultService _getDataSetByProductsID = DBService.GetService<p_ProductsStock_getDataSetByProductsID_ResultService>();
        private readonly IBLL.Ip_Inventory_getSearchList_ResultService _Inventory_getSearchList = DBService.GetService<p_Inventory_getSearchList_ResultService>();
        private readonly IBLL.Ip_Inventory_getSearchListByID_ResultService _Inventory_getSearchListByID = DBService.GetService<p_Inventory_getSearchListByID_ResultService>();

        //管理出库
        private readonly IBLL.Ip_OutStock_getSearchList_ResultService _OutStock_getSearchList = DBService.GetService<p_OutStock_getSearchList_ResultService>();
        private readonly IBLL.Ip_OutStock_getSearchListByID_ResultService _OutStock_getSearchListByID = DBService.GetService<p_OutStock_getSearchListByID_ResultService>();

        //管理入库
        private readonly IBLL.Ip_AppendStock_getSearchList_ResultService _AppendStock_getSearchList = DBService.GetService<p_AppendStock_getSearchList_ResultService>();
        private readonly IBLL.Ip_AppendStock_getSearchListByID_ResultService _AppendStock_getSearchListByID = DBService.GetService<p_AppendStock_getSearchListByID_ResultService>();
        //private readonly IBLL.Ip_AppendStockDetail_getByID_ResultService _AppendStockDetail = DBService.GetService<p_AppendStockDetail_getByID_ResultService>();

        //管理调拨
        private readonly IBLL.Ip_TransferringOrder_getSearchList_ResultService _TransferringOrder_getSearchList = DBService.GetService<p_TransferringOrder_getSearchList_ResultService>();
        private readonly IBLL.Ip_TransferringOrder_getSearchListByID_ResultService _TransferringOrder_getSearchListByID = DBService.GetService<p_TransferringOrder_getSearchListByID_ResultService>();
        private readonly IBLL.It_TransferringOrderService _TransferringOrder = DBService.GetService<t_TransferringOrderService>();

        //采购退货订单 
        private readonly IBLL.Ip_BuyReturn_getBuyReturnOrderList_ResultService _BuyReturn_getBuyReturnOrderList = DBService.GetService<p_BuyReturn_getBuyReturnOrderList_ResultService>();
        private readonly IBLL.Ip_BuyReturn_getListByBuyReturnID_ResultService _BuyReturn_getListByBuyReturnID = DBService.GetService<p_BuyReturn_getListByBuyReturnID_ResultService>();

        //采购付款订单
        private readonly IBLL.Ip_BuyPay_getBuyPayOrderList_ResultService _BuyPay_getBuyPayOrderList = DBService.GetService<p_BuyPay_getBuyPayOrderList_ResultService>();
        private readonly IBLL.Ip_BuyPay_getByBuyReceiptID_ResultService _BuyPay_getByBuyReceiptID = DBService.GetService<p_BuyPay_getByBuyReceiptID_ResultService>();

        //采购收货订单 
        private readonly IBLL.Ip_BuyReceipt_getAdminBuyReceiptOrderList_ResultService _BuyReceipt_getAdminBuy = DBService.GetService<p_BuyReceipt_getAdminBuyReceiptOrderList_ResultService>();
        private readonly IBLL.Ip_BuyReceipt_getListByReceiptOrderID_ResultService _BuyReceipt_getListByRece = DBService.GetService<p_BuyReceipt_getListByReceiptOrderID_ResultService>();
        private readonly IBLL.It_BuyReceiptService _BuyReceipt = DBService.GetService<t_BuyReceiptService>();


        //管理采购订单 
        private readonly IBLL.Ip_BuyOrder_getAdminBuyOrderList_ResultService _BuyOrder_getAdminBuyOrderList = DBService.GetService<p_BuyOrder_getAdminBuyOrderList_ResultService>();
        private readonly IBLL.Ip_BuyOrder_getAdminBuyOrderByBuyOrderID_ResultService _BuyOrder_getAdminBuyOrderByBuyOrderID = DBService.GetService<p_BuyOrder_getAdminBuyOrderByBuyOrderID_ResultService>();
        private readonly IBLL.It_BuyOrderService _BuyOrder = DBService.GetService<t_BuyOrderService>();

        #endregion

        #region erp上的功能
        //请求分类数据
        [HttpPost]
        public ActionResult StroeHouseList()
        {
            var obj = _storeHouseService.LoadEntities(i => true).ToList();

            return Json(obj, JsonRequestBehavior.AllowGet);
        }

        //请求库区数据
        [HttpPost]
        public ActionResult StroeHouseListData(int id)
        {
            var obj = _storeHouseDetail.LoadEntities(i => i.HouseID == id).ToList();

            return Json(obj, JsonRequestBehavior.AllowGet);
        }

        #region 仓库库存情况
        //仓库库存情况
        public ActionResult StockViewModule(int HouseID = 1, int HouseDetailID = 1)
        {
            SqlParameter[] ps = new SqlParameter[]
                {
                    new SqlParameter("@HouseID",HouseID),
                     new SqlParameter("@HouseDetailID",HouseDetailID)
                };

            ViewBag.ProductsStock = _vProductsStock.Procedures<p_ProductsStock_getDataSetByHouseDetailID_Result>("exec p_ProductsStock_getDataSetByHouseDetailID @HouseID,@HouseDetailID", ps);

            return PartialView();
        }
        #endregion

        #region 商品分布情况
        //商品分布情况
        public ActionResult StockProductsView(int ProductsID = 0)
        {
            SqlParameter[] ps = new SqlParameter[]
            {
                new SqlParameter("@ProductsID",ProductsID),
            };

            ViewBag.ProductsStock = _vProductsStock.Procedures<p_ProductsStock_getDataSetByProductsID_Result>("exec p_ProductsStock_getDataSetByProductsID @ProductsID", ps);

            return PartialView();
        }
        #endregion

        #region 管理库存盘点

        //管理库存盘点库单
        public ActionResult ManagerInventory(string InventoryID = "", string beginDate = "2000-1-1", string endDate = "2050-12-12", int sideState = 0)
        {
            if (!string.IsNullOrEmpty(InventoryID))
            {

                SqlParameter[] ps = new SqlParameter[]
                             {
                                new SqlParameter("@InventoryID",InventoryID)
                             };

                ViewBag.GetSearchList = _Inventory_getSearchListByID.Procedures<p_Inventory_getSearchList_Result>("exec p_Inventory_getSearchListByID @InventoryID", ps);

            }
            else
            {
                SqlParameter[] ps = new SqlParameter[]
                                 {
                                    new SqlParameter("@beginDate",beginDate!=""?beginDate:"2000-1-1"),
                                    new SqlParameter("@endDate",endDate!=""?endDate:"2050-12-12"),
                                    new SqlParameter("@sideState",sideState)
                                 };
                ViewBag.GetSearchList = _Inventory_getSearchList.Procedures<p_Inventory_getSearchList_Result>("exec p_Inventory_getSearchList @beginDate,@endDate,@sideState", ps);
            }
            return PartialView();
        }
        //库存盘点库添加页面
        public ActionResult AddInventory()
        {
            return PartialView();
        }
        //添加库存盘点
        [HttpPut]
        public ActionResult ADDEitity(t_Inventory enity)
        {
            var result = Inventory.AddEntity(enity);
            return Json(result);
        }
        //删除库存盘点库单
        [HttpDelete]
        public ActionResult DeleteEitity(string id)
        {
            try
            {
                var list = from a in Inventory.LoadEntities(t => true) where a.InventoryID == id && a.State == 0 select a;

                bool result;

                if (list != null)
                {
                    result = Inventory.DeleEntity(new t_Inventory { InventoryID = id });
                }
                else
                {
                    result = false;
                }
                return Json(new { result });
            }
            catch (Exception e)
            {
                return Json(new { result = "fasle", error = e.ToString() });
            }

        }

        //审核库存盘点库单
        [HttpPost]
        public ActionResult UpdateInventory(string id, int state)
        {
            state = state == 1 ? 0 : 1;

            var result = Inventory.UpdateEntity(new t_Inventory()
            {
                InventoryID = id,
                State = state
            }, i => i.State);

            return Json(result);
        }
        #endregion

        #region 库存出库单
        //库存出库单
        public ActionResult ManagerOutStock(string OutID = "", string beginDate = "2000-1-1", string endDate = "2050-12-12", int sideState = 0)
        {
            if (!string.IsNullOrEmpty(OutID))
            {
                SqlParameter[] ps = new SqlParameter[]
                             {
                                new SqlParameter("@OutID",OutID)
                             };
                ViewBag.GetSearchList = _OutStock_getSearchListByID.Procedures<p_OutStock_getSearchList_Result>("exec p_OutStock_getSearchListByID @OutID", ps);
            }
            else
            {
                SqlParameter[] ps = new SqlParameter[]
                                 {
                                    new SqlParameter("@beginDate",beginDate!=""?beginDate:"2000-1-1"),
                                    new SqlParameter("@endDate",endDate!=""?endDate:"2050-12-12"),
                                    new SqlParameter("@sideState",sideState)
                                 };
                ViewBag.GetSearchList = _OutStock_getSearchList.Procedures<p_OutStock_getSearchList_Result>("exec p_OutStock_getSearchList @beginDate,@endDate,@sideState", ps);
            }

            return PartialView();
        }
        //添加出库单
        [HttpPost]
        public ActionResult ADDOutStock(t_OutStock list)
        {
            var result = _OutStock.AddEntity(list);

            return Json(result);
        }

        //删除出库单
        [HttpDelete]
        public ActionResult DeleteOutStock(string id)
        {
            try
            {
                var list = from a in _OutStock.LoadEntities(t => true) where a.OutID == id && a.State == 0 select a;

                bool result;

                if (list != null)
                {
                    result = _OutStock.DeleEntity(new t_OutStock { OutID = id });
                }
                else
                {
                    result = false;
                }
                return Json(new { result });
            }
            catch (Exception e)
            {
                return Json(new { result = "fasle", error = e.ToString() });
            }
        }

        //审核出库单
        [HttpPost]
        public ActionResult UpdateOutStock(string id, int state)
        {
            state = state == 1 ? 0 : 1;

            var result = _OutStock.UpdateEntity(new t_OutStock()
            {
                OutID = id,
                State = state
            }, i => i.State);

            return Json(result);
        }

        public string changString0(string str)
        {

            if (str.Equals("0"))
            {
                return "报损";
            }
            else if (str.Equals("1"))
            {
                return "借出";
            }
            else if (str.Equals("2"))
            {
                return "借入还出";
            }
            else if (str.Equals("3"))
            {
                return "赠送";
            }
            else if (str.Equals("4"))
            {
                return "其他出仓";
            }
            else
            {
                return "";
            }

        }

        #endregion

        #region 管理入库单

        //管理入库单
        public ActionResult ManagerAppendStock(string OutID = "", string beginDate = "2000-1-1", string endDate = "2050-12-12", int sideState = 0)
        {
            if (!string.IsNullOrEmpty(OutID))
            {
                SqlParameter[] ps = new SqlParameter[]
                             {
                                new SqlParameter("@OutID",OutID)
                             };
                ViewBag.GetSearchList = _AppendStock_getSearchListByID.Procedures<p_AppendStock_getSearchList_Result>("exec p_AppendStock_getSearchListByID @OutID", ps);
            }
            else
            {
                SqlParameter[] ps = new SqlParameter[]
                                 {
                                    new SqlParameter("@beginDate",beginDate!=""?beginDate:"2000-1-1"),
                                    new SqlParameter("@endDate",endDate!=""?endDate:"2050-12-12"),
                                    new SqlParameter("@sideState",sideState)
                                 };

                ViewBag.GetSearchList = _AppendStock_getSearchList.Procedures<p_AppendStock_getSearchList_Result>("exec p_AppendStock_getSearchList @beginDate,@endDate,@sideState", ps);
            }
            return PartialView();
        }

        [HttpPost]
        public ActionResult ManagerAppendStock()
        {
            //添加 p_AppendStock_insertNewEntity

            //删除 p_AppendStock_deleteEitity

            //AuditingOrder方法 p_AppendStock_AuditingOrder 参数AppendID ，UserName

            //全部 p_AppendStock_getSearchList

            //编号查询 p_AppendStock_getSearchListByID

            return PartialView();
        }

        //添加入库单
        [HttpPut]
        public ActionResult ADDAppendStock(t_AppendStock enity)
        {
            var result = _AppendStock.AddEntity(enity);
            return Json(result);
        }
         //删除入库单
        [HttpDelete]
        public ActionResult DeleteAppendStock(string id)
        {
            SqlParameter[] ps = new SqlParameter[]
             {
                  new SqlParameter("@AppendID",id)
             };
           var reust= _AppendStock.Procedures<int>("exec p_AppendStock_deleteEitity @AppendID", ps);

            return Json(reust);
        }
        
        //审核入库单
        [HttpPost]
        public ActionResult UpdateAppendStock(string id, int state)
        {
            state = state == 1 ? 0 : 1;

            var result = _AppendStock.UpdateEntity(new t_AppendStock()
            {
                AppendID = id,
                State = state
            }, i => i.State);
            return Json(result);
        }
        #endregion

        #region 库存调拨

        //库存调拨
        public ActionResult ManagerTransferringOrder(string ID = "", string beginDate = "2000-1-1", string endDate = "2050-12-12", int sideState = 0)
        {
            if (!string.IsNullOrEmpty(ID))
            {
                SqlParameter[] ps = new SqlParameter[]
                             {
                                new SqlParameter("@ID",ID)
                             };
                ViewBag.GetSearchList = _OutStock_getSearchListByID.Procedures<p_TransferringOrder_getSearchList_Result>("exec p_TransferringOrder_getSearchListByID @ID", ps);
            }
            else
            {
                SqlParameter[] ps = new SqlParameter[]
                                 {
                                    new SqlParameter("@beginDate",beginDate!=""?beginDate:"2000-1-1"),
                                    new SqlParameter("@endDate",endDate!=""?endDate:"2050-12-12"),
                                    new SqlParameter("@sideState",sideState)
                                 };

                ViewBag.GetSearchList = _AppendStock_getSearchList.Procedures<p_TransferringOrder_getSearchList_Result>("exec p_TransferringOrder_getSearchList @beginDate,@endDate,@sideState", ps);
            }
            return PartialView();
        }

        //审核库存调拨
        [HttpPost]
        public ActionResult updateTransferringOrder(string id, int state)
        {
            state = state == 1 ? 0 : 1;

            var result = _TransferringOrder.UpdateEntity(new  t_TransferringOrder()
            {
                ID = id,
                State = state
            }, i => i.State);

            return Json(result);
        }

        //删除库存调拨
        [HttpDelete]
        public ActionResult DeleteTransferringOrder(string id)
        {
           var result = _TransferringOrder.DeleEntity(new t_TransferringOrder { ID = id });

            return Json(result);
        }

        [HttpPost]
        public ActionResult ManagerTransferringOrder(int ud)
        {

            //添加 p_TransferringOrder_insertNewEntity

            //删除 p_TransferringOrder_deleteEitity

            //AuditingOrder方法 p_TransferringOrder_AuditingOrder 参数AppendID ，UserName

            //全部 p_TransferringOrder_getSearchList

            //编号查询 p_TransferringOrder_getSearchListByID

            return PartialView();
        }
        #endregion

        #region 采购退货订单
        //采购退货订单
        public ActionResult ManagerBuyReturn(string BuyReturnID = "", string beginDate = "2000-1-1", string endDate = "2050-12-12", int sideState = 0)
        {
            if (!string.IsNullOrEmpty(BuyReturnID))
            {
                SqlParameter[] ps = new SqlParameter[]
                             {
                                new SqlParameter("@BuyReturnID",BuyReturnID)
                             };
                ViewBag.GetSearchList = _OutStock_getSearchListByID.Procedures<p_BuyReturn_getBuyReturnOrderList_Result>("exec p_BuyReturn_getListByBuyReturnID @BuyReturnID", ps);
            }
            else
            {
                SqlParameter[] ps = new SqlParameter[]
                                 {
                                    new SqlParameter("@beginDate",beginDate!=""?beginDate:"2000-1-1"),
                                    new SqlParameter("@endDate",endDate!=""?endDate:"2050-12-12"),
                                    new SqlParameter("@sideState",sideState)
                                 };

                ViewBag.GetSearchList = _AppendStock_getSearchList.Procedures<p_BuyReturn_getBuyReturnOrderList_Result>("exec p_BuyReturn_getBuyReturnOrderList @beginDate,@endDate,@sideState", ps);
            }
            return PartialView();
        }

        [HttpPost]
        public ActionResult ManagerBuyReturn()
        {
            //p_BuyReturn_getBuyReturnOrderList

            return PartialView();
        }

        #endregion

        #region 采购付款订单
        //采购付款订单
        public ActionResult ManagerBuyPar(string BuyReturnID = "", string beginDate = "2000-1-1", string endDate = "2050-12-12", int sideState = 0)
        {
            if (!string.IsNullOrEmpty(BuyReturnID))
            {
                SqlParameter[] ps = new SqlParameter[]
                             {
                                new SqlParameter("@BuyReturnID",BuyReturnID)
                             };
                ViewBag.GetSearchList = _OutStock_getSearchListByID.Procedures<p_BuyReturn_getBuyReturnOrderList_Result>("exec p_BuyReturn_getListByBuyReturnID @BuyReturnID", ps);
            }
            else
            {
                SqlParameter[] ps = new SqlParameter[]
                                 {
                                    new SqlParameter("@beginDate",beginDate!=""?beginDate:"2000-1-1"),
                                    new SqlParameter("@endDate",endDate!=""?endDate:"2050-12-12"),
                                    new SqlParameter("@sideState",sideState)
                                 };

                ViewBag.GetSearchList = _AppendStock_getSearchList.Procedures<p_BuyReturn_getBuyReturnOrderList_Result>("exec p_BuyReturn_getBuyReturnOrderList @beginDate,@endDate,@sideState", ps);
            }
            return PartialView();
        }

        [HttpPost]
        public ActionResult ManagerBuyPar()
        {
            // p_BuyPay_getBuyPayOrderList

            return PartialView();
        }
        #endregion

        #region 采购收货订单
        //采购收货订单
        public ActionResult ManagerBuyReceipt(string ReceiptOrderID = "", string beginDate = "2000-1-1", string endDate = "2050-12-12", int sideState = 0)
        {
            if (!string.IsNullOrEmpty(ReceiptOrderID))
            {
                SqlParameter[] ps = new SqlParameter[]
                             {
                                new SqlParameter("@ReceiptOrderID",ReceiptOrderID)
                             };
                ViewBag.GetSearchList = _BuyReceipt.Procedures<p_BuyReceipt_getAdminBuyReceiptOrderList_Result>("exec p_BuyReceipt_getListByReceiptOrderID @ReceiptOrderID", ps);
            }
            else
            {
                SqlParameter[] ps = new SqlParameter[]
                                 {
                                    new SqlParameter("@beginDate",beginDate!=""?beginDate:"2000-1-1"),
                                    new SqlParameter("@endDate",endDate!=""?endDate:"2050-12-12"),
                                    new SqlParameter("@sideState",sideState)
                                 };

                ViewBag.GetSearchList = _BuyReceipt.Procedures<p_BuyReceipt_getAdminBuyReceiptOrderList_Result>("exec p_BuyReceipt_getAdminBuyReceiptOrderList @beginDate,@endDate,@sideState", ps);
            }
            return PartialView();
        }

        //审核
        [HttpPost]
        public ActionResult UpdateManagerBuyReceipt(string id, int state)
        {
            state = state == 1 ? 0 : 1;

            var result = _BuyReceipt.UpdateEntity(new t_BuyReceipt()
            {
                ReceiptOrderID = id,
                State = state
            }, i => i.State);
            return Json(result);
        }

        //删除
        [HttpDelete]
        public ActionResult DeleteManagerBuyReceipt(string id)
        {
            var result = _BuyReceipt.Procedures<int>("exec p_BuyReceipt_deleteEntity @ReceiptOrderID", new SqlParameter[]
            {
                new SqlParameter { ParameterName = "@ReceiptOrderID", Value = id}
            });
            return Json(result);
        }

        [HttpPost]
        public ActionResult ManagerBuyReceipt()
        {
            // p_BuyReceipt_getAdminBuyReceiptOrderList
            return PartialView();
        }
        #endregion

        #region 管理采购订单
        //管理采购订单

        public ActionResult ManagerBuyOrder(string BuyOrderID = "", string beginDate = "2000-1-1", string endDate = "2050-12-12", int sideState = 0)
        {
            if (!string.IsNullOrEmpty(BuyOrderID))
            {
                SqlParameter[] ps = new SqlParameter[]
                             {
                                new SqlParameter("@BuyOrderID",BuyOrderID)
                             };
                ViewBag.GetSearchList = _BuyOrder.Procedures<p_BuyOrder_getAdminBuyOrderList_Result>("exec p_BuyOrder_getAdminBuyOrderByBuyOrderID @BuyOrderID", ps);
            }
            else
            {
                int Identitys = 0;
                SqlParameter[] ps = new SqlParameter[]
                                 {
                                    new SqlParameter("@beginDate",beginDate!=""?beginDate:"2000-1-1"),
                                    new SqlParameter("@endDate",endDate!=""?endDate:"2050-12-12"),
                                    new SqlParameter("@sideState",sideState),
                                    new SqlParameter("@Identitys",Identitys)                                  
                                 };

                ViewBag.GetSearchList = _BuyOrder.Procedures<p_BuyOrder_getAdminBuyOrderList_Result>("exec p_BuyOrder_getAdminBuyOrderList @beginDate,@endDate,@sideState,@Identitys", ps);
            }
            return PartialView();
        }
        //审核
        [HttpPost]
        public ActionResult UpdateManagerBuyOrder(string id, int state)
        {
            state = state == 1 ? 0 : 1;

            var result = _BuyOrder.UpdateEntity(new t_BuyOrder()
            {
                BuyOrderID = id,
                State = state
            }, i => i.State);

            return Json(result);
        }
        //删除
        [HttpDelete]
        public ActionResult DeleteManagerBuyOrder(string id)
        {
            var result = _BuyOrder.Procedures<int>("exec p_BuyOrder_deleteBuyOrder @BuyOrderID", new SqlParameter[]
            {
                new SqlParameter { ParameterName = "@BuyOrderID", Value = id}
            });

            return Json(result);
        }

        [HttpPost]
        public ActionResult ManagerBuyOrder()
        {
            //p_BuyOrder_getAdminBuyOrderList
            return PartialView();
        }
        #endregion

        #endregion
    }
}
